﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace DevelopTools.UI
{
    /// <summary>
    /// UI文字控件
    /// </summary>
    [Serializable]
    public class UIText : MonoBehaviour
    {
        /// <summary>
        /// 文字类型 
        /// </summary>
        public TextType textType = TextType.TextImage;
        /// <summary>
        /// UGUI的text组件
        /// </summary>
        public Text textUGUI;
        /// <summary>
        /// TextMesh组件
        /// </summary>
        public TextMeshProUGUI textMeshUGUI;
        /// <summary>
        /// 使用的文字图片
        /// </summary>
        public Image textTexture;
        /// <summary>
        /// 存放的UI
        /// </summary>
        public Image changeImage;
        /// <summary>
        /// UIText中存放的UI中的文字
        /// </summary>
        public string text;

        private void Awake()
        {
            switch (textType)
            {
                case TextType.UGUIText:
                    textUGUI = GetComponentInChildren<Text>();
                    break;
                case TextType.TextMeshText:
                    textMeshUGUI = GetComponentInChildren<TextMeshProUGUI>();
                    break;
                case TextType.TextImage:
                    changeImage = GetComponentInChildren<Image>();
                    break;
            }
        }

        /// <summary>
        /// 更新文字
        /// </summary>
        /// <param name="inputStr">替换的文字</param>
        public void UpdateText(string inputStr)
        {
            switch (textType)
            {
                case TextType.UGUIText:
                    ChangeUGUIText();
                    break;
                case TextType.TextMeshText:
                    ChangeTextMeshText();
                    break;
            }
        }
        /// <summary>
        /// 更新文字
        /// </summary>
        /// <param name="inputImage">替换的图片</param>
        public void UpdateText(Image inputImage)
        {
            if (textType == TextType.TextImage)
            {
                ChangeImageText();
            }
        }
        /// <summary>
        /// 更新文字：使用公开方法更替文字变量、图片变量
        /// </summary>
        public void UpdateText()
        {
            switch (textType)
            {
                case TextType.UGUIText:
                    ChangeUGUIText();
                    break;
                case TextType.TextMeshText:
                    ChangeTextMeshText();
                    break;
                case TextType.TextImage:
                    ChangeImageText();
                    break;
            }
        }
        public void ChangeUGUIText()
        {
            //文字替换
            textUGUI.text = text;
        }

        public void ChangeTextMeshText()
        {
            //文字替换
            textMeshUGUI.text = text;
        }

        public void ChangeImageText()
        {
            //替换图片
            textTexture.gameObject.SetActive(false);
            textTexture = changeImage;
        }

        /// <summary>
        /// 使用的文件组件-类型
        /// </summary>
        public enum TextType
        {
            UGUIText,//UGUI中自带的text
            TextMeshText,//TextMesh插件的text
            TextImage,//带文字的Image
        }
    }
}